import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author Stringzhua
 * @Date 2024/11/1 10:19
 * description:
 */
public class Demo01 {
    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    public static ThreadLocal<Integer> threadLocalInt = new ThreadLocal<>();

    public static void main(String[] args) {
        show show = new show();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    threadLocal.set("妲己");
                    threadLocalInt.set(10000);
                    show.show();
                    show.choose();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    threadLocal.remove();
                    threadLocalInt.remove();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    threadLocal.set("张飞");
                    threadLocalInt.set(100);
                    show.show();
                    show.choose();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    threadLocal.remove();
                    threadLocalInt.remove();
                }
            }
        });
        t1.start();
        t2.start();
    }
}

class show {
    public void show() {
        String role = Demo01.threadLocal.get();
        Integer score = Demo01.threadLocalInt.get();
        System.out.println(Thread.currentThread().getName() + "当前角色为" + role + "积分" + score);
    }

    public void choose() {
        String role = Demo01.threadLocal.get();
        Integer score = Demo01.threadLocalInt.get();
        System.out.println(Thread.currentThread().getName() + "选择的角色为" + role + "积分" + score);
    }
}
